package com.cal.utms.model.maintenance;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.cal.utms.dao.maintenance.IssuerDaoImpl;
import com.cal.utms.dto.maintenance.CreditRatingDto;
import com.cal.utms.dto.maintenance.IssuerBankAccountDto;
import com.cal.utms.dto.maintenance.IssuerContactPersonDto;
import com.cal.utms.dto.maintenance.IssuerDto;
import com.cal.utms.dto.maintenance.IssuerTypeDto;
import com.cal.utms.entity.CreditRating;
import com.cal.utms.entity.Issuer;
import com.cal.utms.entity.IssuerBankAccount;
import com.cal.utms.entity.IssuerContactPerson;
import com.cal.utms.entity.IssuerType;

public class IssuerModel {

	// btn-default-large-disabled-sides.gif
	// btn-default-large-disabled-sides.gif

	private IssuerDaoImpl daoImpl = new IssuerDaoImpl();

	public static Issuer createIssuerModel(IssuerDto dto, String bankAccounts, String contactPersons) {
		Issuer issuer = new Issuer();
		issuer.setName(dto.getName());
		issuer.setCode(dto.getCode());
		issuer.setAddress(dto.getAddress());
		issuer.setPhone(dto.getPhone());
		issuer.setFax(dto.getFax());
		issuer.setEmail(dto.getEmail());
		issuer.setRatingGivenBy(dto.getRatingGivenBy());
		issuer.setCreditRating(dto.getCreditRating());
		issuer.setIssuerType(dto.getIssuerTypeId());
		issuer.setCompanyRegNo(dto.getCompanyRegNo());

		if (bankAccounts != null) {
			bankAccounts = "{ \"bankAccounts\" : ".concat(bankAccounts) + " }";
			List<IssuerBankAccount> accountDtos = new ArrayList<IssuerBankAccount>();
			try {
				JSONObject jsonObject = new JSONObject(bankAccounts);
				JSONArray jArray = jsonObject.getJSONArray("bankAccounts");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					IssuerBankAccount accountDto = new IssuerBankAccount();
					accountDto.setAccountNo(jObj.getString("accountNo"));
					accountDto.setBarnch(jObj.getString("barnch"));
					accountDto.setNameOfBank(jObj.getString("nameOfBank"));
					accountDto.setIssuer(issuer);
					accountDtos.add(accountDto);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			issuer.setIssuerBankAccounts(accountDtos);
		}

		if (contactPersons != null) {
			contactPersons = "{ \"contactPersons\" : ".concat(contactPersons) + " }";
			List<IssuerContactPerson> issuerContactPersons = new ArrayList<IssuerContactPerson>();
			try {
				JSONObject jsonObject = new JSONObject(contactPersons);
				JSONArray jArray = jsonObject.getJSONArray("contactPersons");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					IssuerContactPerson contactPerson = new IssuerContactPerson();
					contactPerson.setCpName(jObj.getString("cpName"));
					contactPerson.setCpDesignation(jObj.getString("cpDesignation"));
					contactPerson.setCpEmail(jObj.getString("cpEmail"));
					contactPerson.setCpFax(jObj.getString("cpFax"));
					contactPerson.setCpPhone(jObj.getString("cpPhone"));
					contactPerson.setIssuer(issuer);
					issuerContactPersons.add(contactPerson);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			issuer.setIssuerContactPersons(issuerContactPersons);
		}
		return issuer;
	}

	public static IssuerType createIssuerTypeModel(IssuerTypeDto issuerTypeDto) {
		IssuerType issuerType = new IssuerType();
		issuerType.setId(issuerTypeDto.getId());
		issuerType.setCode(issuerTypeDto.getCode());
		issuerType.setIssureCategory(issuerTypeDto.getIssureCategory());
		issuerType.setPrimaryDealer(issuerTypeDto.getPrimaryDealer());
		issuerType.setMaxinvestment(issuerTypeDto.getMaxinvestment());
		return issuerType;
	}

	public List<IssuerDto> createIssuerDtoList(List<Issuer> issures) {
		List<IssuerDto> issuerDtos = new ArrayList<IssuerDto>();
		for (Issuer issuer : issures) {
			IssuerDto issuerDto = new IssuerDto();
			issuerDto.setId(issuer.getId());
			issuerDto.setCode(issuer.getCode());
			issuerDto.setName(issuer.getName());
			issuerDto.setAddress(issuer.getAddress());
			issuerDto.setPhone(issuer.getPhone());
			issuerDto.setFax(issuer.getFax());
			issuerDto.setEmail(issuer.getEmail());
			issuerDto.setIssuerTypeId(issuer.getIssuerType());
			issuerDto.setCreditRating(issuer.getCreditRating());
			issuerDto.setRatingGivenBy(issuer.getRatingGivenBy());
			issuerDto.setCompanyRegNo(issuer.getCompanyRegNo());
			issuerDtos.add(issuerDto);
		}
		return issuerDtos;
	}

	public static List<IssuerTypeDto> createIssuerTypeDtoList(List<IssuerType> issuerTypes) {
		List<IssuerTypeDto> issuerTypeDtos = new ArrayList<IssuerTypeDto>();
		for (IssuerType issuerType : issuerTypes) {
			IssuerTypeDto issuerTypeDto = new IssuerTypeDto();
			issuerTypeDto.setId(issuerType.getId());
			issuerTypeDto.setCode(issuerType.getCode());
			issuerTypeDto.setIssureCategory(issuerType.getIssureCategory());
			issuerTypeDto.setPrimaryDealer(issuerType.getPrimaryDealer());
			issuerTypeDto.setMaxinvestment(issuerType.getMaxinvestment());
			issuerTypeDtos.add(issuerTypeDto);
		}
		return issuerTypeDtos;
	}

	public static List<CreditRatingDto> createCreditRateList(List<CreditRating> creditRatings) {
		List<CreditRatingDto> creditRatingDtos = new ArrayList<CreditRatingDto>();
		for (CreditRating creditRating : creditRatings) {
			CreditRatingDto creditRatingDto = new CreditRatingDto();
			creditRatingDto.setId(creditRating.getId());
			creditRatingDto.setCode(creditRating.getCode());
			creditRatingDto.setCreditRating(creditRating.getCreditRating());
			creditRatingDto.setRatingValue(creditRating.getRatingValue());
			creditRatingDtos.add(creditRatingDto);
		}
		return creditRatingDtos;
	}

	public static List<IssuerBankAccount> createBankAccountList(List<IssuerBankAccountDto> accountDtos) {
		List<IssuerBankAccount> accounts = new ArrayList<IssuerBankAccount>();
		for (IssuerBankAccountDto issuerBankAccountDto : accountDtos) {
			IssuerBankAccount account = new IssuerBankAccount();
			account.setAccountNo(issuerBankAccountDto.getAccountNo());
			account.setBarnch(issuerBankAccountDto.getBarnch());
			account.setNameOfBank(issuerBankAccountDto.getNameOfBank());
			account.setIssuer(issuerBankAccountDto.getIssure());
			accounts.add(account);
		}
		return accounts;
	}

	public String saveIssuer(IssuerDto issuerDto, String bankAccounts, String contactPersons) {

		Issuer perIssuer = daoImpl.getIssuerByIssuerCode(issuerDto.getCode());
		if (perIssuer != null) {
			issuerDto.setSuccess(false);
			issuerDto.setResultMsg("Issuer Code already exists");
		} else {
			Issuer issuer = createIssuerModel(issuerDto, bankAccounts, contactPersons);
			Object object = daoImpl.saveIssuer(issuer);
			if (object instanceof Integer) {
				issuerDto.setId((Integer) object);
				issuerDto.setSuccess(true);
				issuerDto.setResultMsg("SUCCESS");
			} else {
				issuerDto.setSuccess(false);
				issuerDto.setResultMsg("There is some error returned from the server.");
			}
		}
		return issuerDto.getResultMsg();
	}

	public List<IssuerBankAccountDto> getBankAcountsByIssuer(int issuer_id) {
		List<IssuerBankAccountDto> bankAccountDtos = new ArrayList<IssuerBankAccountDto>();
		List<IssuerBankAccount> bankAccounts = daoImpl.getBankAccountByIssuer(issuer_id);
		for (IssuerBankAccount issuerBankAccount : bankAccounts) {
			IssuerBankAccountDto accountDto = new IssuerBankAccountDto();
			accountDto.setAccountNo(issuerBankAccount.getAccountNo());
			accountDto.setBarnch(issuerBankAccount.getBarnch());
			accountDto.setId(issuerBankAccount.getId());
			accountDto.setNameOfBank(issuerBankAccount.getNameOfBank());
			bankAccountDtos.add(accountDto);
		}
		return bankAccountDtos;
	}

	public List<IssuerContactPersonDto> getContactsByIssuer(int issuer_id) {
		List<IssuerContactPersonDto> contactPersonDtos = new ArrayList<IssuerContactPersonDto>();
		List<IssuerContactPerson> contactPersons = daoImpl.getContactsByIssuer(issuer_id);
		for (IssuerContactPerson issuerContactPerson : contactPersons) {
			IssuerContactPersonDto contactPersonDto = new IssuerContactPersonDto();
			contactPersonDto.setCpName(issuerContactPerson.getCpName());
			contactPersonDto.setCpDesignation(issuerContactPerson.getCpDesignation());
			contactPersonDto.setCpEmail(issuerContactPerson.getCpEmail());
			contactPersonDto.setCpFax(issuerContactPerson.getCpFax());
			contactPersonDto.setCpPhone(issuerContactPerson.getCpPhone());
			contactPersonDto.setId(issuerContactPerson.getId());
			contactPersonDtos.add(contactPersonDto);
		}

		return contactPersonDtos;
	}

	public String update(int issuerId, IssuerDto issuerDto, String bankAccounts, String contactPersons) {

		Issuer perIssuer = daoImpl.getIssuerByIssuerCode(issuerDto.getCode());
		if (perIssuer != null) {
			if (perIssuer.getId() != issuerId) {
				issuerDto.setSuccess(false);
				issuerDto.setResultMsg("Issuer Code already exists");
			} else {
				updateIssuer(issuerId, issuerDto, bankAccounts, contactPersons);
			}
		} else {
			updateIssuer(issuerId, issuerDto, bankAccounts, contactPersons);
		}

		return issuerDto.getResultMsg();
	}

	public void updateIssuer(int issuerId, IssuerDto issuerDto, String bankAccounts, String contactPersons) {
		List<IssuerBankAccount> accountDtos = new ArrayList<IssuerBankAccount>();
		Issuer issuer = createIssuerModel(issuerDto, null, contactPersons);
		issuer.setId(issuerId);
		if (bankAccounts != null) {
			bankAccounts = "{ \"bankAccounts\" : ".concat(bankAccounts) + " }";

			try {
				JSONObject jsonObject = new JSONObject(bankAccounts);
				JSONArray jArray = jsonObject.getJSONArray("bankAccounts");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					IssuerBankAccount accountDto = new IssuerBankAccount();
					if (jObj.getInt("id") != 0) {
						accountDto.setId(jObj.getInt("id"));
					}
					accountDto.setAccountNo(jObj.getString("accountNo"));
					accountDto.setBarnch(jObj.getString("barnch"));
					accountDto.setNameOfBank(jObj.getString("nameOfBank"));
					accountDto.setIssuer(issuer);
					accountDtos.add(accountDto);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		@SuppressWarnings("unused")
		Object object = daoImpl.updateIssuer(issuer, accountDtos);
		issuerDto.setSuccess(true);
		issuerDto.setResultMsg("SUCCESS");
	}

	public void saveIssuerCategory(String categories, IssuerTypeDto issuerTypeDto) {
		List<IssuerType> categoriesList = new ArrayList<IssuerType>();
		if (categories != null) {
			categories = "{ \"categories\" : ".concat(categories) + " }";
			try {
				JSONObject jsonObject = new JSONObject(categories);
				JSONArray jArray = jsonObject.getJSONArray("categories");
				for (int i = 0; i < jArray.length(); i++) {
					JSONObject jObj = jArray.getJSONObject(i);
					IssuerType issuerType = new IssuerType();
					if (jObj.getInt("id") != 0) {
						issuerType.setId(jObj.getInt("id"));
					}
					issuerType.setCode(jObj.getString("code"));
					issuerType.setIssureCategory(jObj.getString("issureCategory"));
					issuerType.setMaxinvestment(jObj.getInt("maxinvestment"));
					issuerType.setPrimaryDealer(jObj.getBoolean("primaryDealer"));
					categoriesList.add(issuerType);
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}

			Object object = daoImpl.saveCategory(categoriesList);

			issuerTypeDto.setSuccess(true);
			issuerTypeDto.setResultMsg("SUCCESS");

		}

	}

	public void getUnmapingIssuer(IssuerDto issuerDto) {
		List<Issuer> issuers = daoImpl.getUnmappedIssuers();
		if (issuers.size() > 0) {
			issuerDto.setIssuerDtos(this.createIssuerDtoList(issuers));
			issuerDto.setSuccess(true);
			issuerDto.setResultMsg("SUCCESS");
		}
	}
}
